(*
  Copyright (c) 2009 Barry Schwartz

  Permission is hereby granted, free of charge, to any person
  obtaining a copy of this software and associated documentation
  files (the "Software"), to deal in the Software without
  restriction, including without limitation the rights to use,
  copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the
  Software is furnished to do so, subject to the following
  conditions:

  The above copyright notice and this permission notice shall be
  included in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  OTHER DEALINGS IN THE SOFTWARE.
*)

open XNum
  
let digits_lc =
  [| 48; 49; 50; 51; 52; 53; 54; 55; 56; 57; 97; 98; 99; 100; 101; 102 |]
  
let digits_uc =
  [| 48; 49; 50; 51; 52; 53; 54; 55; 56; 57; 65; 66; 67; 68; 69; 70 |]
  
let rec num_to_ar digits base x =
  if lt_num x num_zero then
    45 :: (num_to_ar digits base (minus_num x))
  else if eq_num x num_zero then
    [ 48 ]
  else
    let b = num_of_int base in
    let rec iter str x =
      if eq_num x num_zero then
        str
      else
        let d = mod_num x b in
        let y = quo_num x b in
          iter (digits.(int_of_num d) :: str) y
    in
      iter [] x

let num_to_arabic base x = num_to_ar digits_lc base x
let num_to_ARABIC base x = num_to_ar digits_uc base x
  
let roman_digit d i v x =
  match d with
    | 1 -> [ i ]
    | 2 -> [ i; i ]
    | 3 -> [ i; i; i ]
    | 4 -> [ i; v ]
    | 5 -> [ v ]
    | 6 -> [ v; i ]
    | 7 -> [ v; i; i ]
    | 8 -> [ v; i; i; i ]
    | 9 -> [ i; x ]
    | _ -> []
  
let rec num_to_rm m d c l x v i n =
  if lt_num n num_zero then
    45 :: (num_to_rm m d c l x v i (minus_num n)) (* - n *)
  else if eq_num n num_zero then
    [ 48 ]                              (* 0 *)
  else
    let mm = int_of_num (quo_num n (num_of_int 1000)) in
    let z = int_of_num (mod_num n (num_of_int 1000)) in
    let cc = z / 100 in
    let xx = (z / 10) - (10 * cc) in
    let ii = (z - (100 * cc)) - (10 * xx) in
      (XList.repeat mm m) @             (* m *)
        ((roman_digit cc c d m) @       (* c d m *)
           ((roman_digit xx x l c) @    (* x l c *)
              (roman_digit ii i v x)))  (* i v x *)
  

let num_to_roman n = num_to_rm 109 100 99 108 120 118 105 n
let num_to_ROMAN n = num_to_rm 77 68 67 76 88 86 73 n

let num_26 = num_of_int 26

let rec num_to_alpha first n =
  if lt_num n num_zero then
    45 :: (num_to_alpha first (minus_num n))
  else if eq_num n num_zero then
    [ 48 ]
  else
    let k = quo_num (sub_num n num_one) num_26 in
    let i = mod_num (sub_num n num_one) num_26 in
      XList.repeat ((int_of_num k) + 1) (first + (int_of_num i))
  
let num_to_alphabetic n = num_to_alpha 97 n
let num_to_ALPHABETIC n = num_to_alpha 65 n
  

